using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Zsa.Reflection.Discovery
{
    /// <summary>
    /// Used to define filters for a type.
    /// </summary>
    /// 
    public class TypeSearchCriteria : ITypeFilter, IList<TypeSearchParameter>
    {
        #region Fields
        /// <summary>
        /// The type search parameters.
        /// </summary>
        private List<TypeSearchParameter> _Parameters;
        #endregion Fields

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="TypeSearchCriteria"/> class.
        /// </summary>
        /// <param name="searchParameters">The search parameters.</param>
        public TypeSearchCriteria()
            : this ( new List<TypeSearchParameter>() )
        {
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="TypeSearchCriteria"/> class.
        /// </summary>
        /// <param name="searchParameters">The search parameters.</param>
        public TypeSearchCriteria(params TypeSearchParameter[] searchParameters)
            : this ( new List<TypeSearchParameter>(searchParameters) )
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TypeSearchCriteria"/> class.
        /// </summary>
        /// <param name="searchParameters">The search parameters.</param>
        private TypeSearchCriteria(List<TypeSearchParameter> searchParameters)
        {
            _Parameters = searchParameters;
        }
        #endregion Constructor

        #region Properties
        /// <summary>
        /// Gets the filter.
        /// </summary>
        /// <value>The filter.</value>
        object ITypeFilter.Filter
        {
            get 
            { 
                return this;
            }
        }

        /// <summary>
        /// Gets or sets the <see cref="Zsa.Reflection.Discovery.TypeSearchParameter"/> at the specified index.
        /// </summary>
        /// <value></value>
        public TypeSearchParameter this[int index]
        {
            get
            {
                return _Parameters[index];
            }
            set
            {
                _Parameters[index] = value;
            }
        }

        /// <summary>
        /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
        /// </summary>
        /// <value></value>
        /// <returns>The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</returns>
        public int Count
        {
            get
            {
                return _Parameters.Count;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only; otherwise, false.</returns>
        bool ICollection<TypeSearchParameter>.IsReadOnly
        {
            get
            {
                return (_Parameters as ICollection<TypeSearchParameter>).IsReadOnly;
            }
        }

        #endregion Properties

        #region Methods
        /// <summary>
        /// Checks the type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        bool ITypeFilter.CheckType(Type type, object filter)
        {
            for (int _TempIndex = 0; _TempIndex < _Parameters.Count; _TempIndex++)
            {
                if (!_Parameters[_TempIndex].IsValid(type))
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// Adds the specified search parameter type.
        /// </summary>
        /// <param name="searchParameterType">Type of the search parameter.</param>
        /// <param name="value">The value.</param>
        public void Add(TypeSearchParameterType searchParameterType,
            object value)
        {
            Add(new TypeSearchParameter(searchParameterType, value));
        }

        /// <summary>
        /// Adds the specified parameter.
        /// </summary>
        /// <param name="parameter">The parameter.</param>
        public void Add(TypeSearchParameter parameter)
        {
            _Parameters.Add(parameter);
        }

        /// <summary>
        /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
        /// </summary>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only. </exception>
        public void Clear()
        {
            _Parameters.Clear();
        }


        /// <summary>
        /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
        /// </summary>
        /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
        /// <returns>
        /// true if item was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false. This method also returns false if item is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"></see>.
        /// </returns>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>
        public bool Remove(TypeSearchParameter item)
        {
            return _Parameters.Remove(item);
        }
        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator<TypeSearchParameter> GetEnumerator()
        {
            return _Parameters.GetEnumerator();
        }


        /// <summary>
        /// Removes the <see cref="T:System.Collections.Generic.IList`1"></see> item at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index of the item to remove.</param>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"></see> is read-only.</exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"></see>.</exception>
        public void RemoveAt(int index)
        {
            _Parameters.RemoveAt(index);
        }


        /// <summary>
        /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> contains a specific value.
        /// </summary>
        /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
        /// <returns>
        /// true if item is found in the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false.
        /// </returns>
        bool ICollection<TypeSearchParameter>.Contains(TypeSearchParameter item)
        {
            return _Parameters.Contains(item);
        }

        /// <summary>
        /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"></see> to an <see cref="T:System.Array"></see>, starting at a particular <see cref="T:System.Array"></see> index.
        /// </summary>
        /// <param name="array">The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"></see>. The <see cref="T:System.Array"></see> must have zero-based indexing.</param>
        /// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">arrayIndex is less than 0.</exception>
        /// <exception cref="T:System.ArgumentNullException">array is null.</exception>
        /// <exception cref="T:System.ArgumentException">array is multidimensional.-or-arrayIndex is equal to or greater than the length of array.-or-The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"></see> is greater than the available space from arrayIndex to the end of the destination array.-or-Type T cannot be cast automatically to the type of the destination array.</exception>
        void ICollection<TypeSearchParameter>.CopyTo(TypeSearchParameter[] array, int arrayIndex)
        {
            _Parameters.CopyTo(array, arrayIndex);
        }



        /// <summary>
        /// Inserts an item to the <see cref="T:System.Collections.Generic.IList`1"></see> at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which item should be inserted.</param>
        /// <param name="item">The object to insert into the <see cref="T:System.Collections.Generic.IList`1"></see>.</param>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"></see> is read-only.</exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">index is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"></see>.</exception>
        void IList<TypeSearchParameter>.Insert(int index, TypeSearchParameter item)
        {
            _Parameters.Insert(index, item);
        }

        /// <summary>
        /// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1"></see>.
        /// </summary>
        /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1"></see>.</param>
        /// <returns>
        /// The index of item if found in the list; otherwise, -1.
        /// </returns>
        int IList<TypeSearchParameter>.IndexOf(TypeSearchParameter item)
        {
            return _Parameters.IndexOf(item);
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
        /// </returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _Parameters.GetEnumerator();
        }


        #endregion Methods
    }
}
